//package com.coinworld.admin.filter;
//
//import com.coinworld.constants.RedisIndex;
//import com.coinworld.exception.BusinessException;
//import com.coinworld.redis.RedisPool;
//import com.coinworld.response.ResponseCode;
//import org.apache.http.HttpStatus;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.Resource;
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.HashSet;
//import java.util.Set;
//
///**
// * Created by ZhangRuiFeng on 2019/5/11.
// */
//
//@WebFilter(filterName = "SepSecurity", urlPatterns = { "/*"})
//public class SepLoginFilter implements Filter {
//
//    @Resource
//    private RedisPool redisPool;
//    /**
//     * 忽略不过滤的路径
//     */
//    private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
//            Arrays.asList("/account/","/sys/","/upload/","/layui","/jquery","/favicon")));
//
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse resp = (HttpServletResponse) servletResponse;
//        if ("OPTIONS".equals(request.getMethod())){
//            resp.setStatus(HttpStatus.SC_NO_CONTENT);
//        }else{
//            String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
//            boolean isFilter = true;
//            for (String allowedPath : ALLOWED_PATHS) {
//                if (path.contains(allowedPath)){
//                    isFilter = false;
//                }
//            }
//            if (isFilter) {
//                String headerToken = request.getHeader("token");
//                //兼容导出
//                String reqToken = request.getParameter("token");
//                if (StringUtils.isEmpty(headerToken) && StringUtils.isEmpty(reqToken)){
//                    throw new BusinessException(ResponseCode.Common.LOGIN_INVALID);
//                }
//                String token = !StringUtils.isEmpty(headerToken)?headerToken:reqToken;
//                token = token.replaceAll(" ","+");
//                String userAccount = redisPool.get(token, RedisIndex.ONE);
//                if (StringUtils.isEmpty(userAccount)) {
//                    throw new BusinessException(ResponseCode.Common.LOGIN_INVALID);
//                }
//                redisPool.expire(token,60 * 60 * 5, RedisIndex.ONE);
//            }
//        }
//        filterChain.doFilter(servletRequest, servletResponse);
//    }
//
//    @Override
//    public void destroy() {
//
//    }
//}